package com.supertimetable.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Admin on 2018/1/12.
 */

public class AutoTextViewAdapter<T> extends BaseAdapter implements Filterable {

    private List<T> mOriginalValue;
    private List<T> mObject;
    private final Object mLock = new Object();
    private int mResoure;
    private MyFilter myFilter;
    private LayoutInflater inflater;

    public AutoTextViewAdapter(Context context, int TextViewResouceId, List<T> objects) {//////////////////////////////////
        init(context,TextViewResouceId,objects);
    }

    private void init(Context context, int textViewResouceId, List<T> objects) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mObject = objects;
        mResoure = textViewResouceId;
        myFilter = new MyFilter();
    }
///////////////////////////////////

    @Override
    public int getCount() {
        return mObject.size();
    }

    @Override
    public Object getItem(int position) {
        return mObject.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        /////////////////////////////////////////////////////////////

        return getViewFromResouce(position,convertView,parent,mResoure);
    }
    private View getViewFromResouce(int position, View convertView, ViewGroup parent , int mResoure2){
        if(convertView == null){
            convertView = inflater.inflate(mResoure2,parent,false);
        }
        TextView tv = (TextView) convertView;
        T item = (T) getItem(position);
        if(item instanceof CharSequence){
            tv.setText((CharSequence)item);
        }else{
            tv.setText((CharSequence)item);
        }
        return tv;
    }
    @Override
    public Filter getFilter() {
        return myFilter;
    }



    private class MyFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if(mOriginalValue == null){
                synchronized (mLock){
                    mOriginalValue = new ArrayList<T>(mObject);
                }
            }
            int count = mOriginalValue.size();
            ArrayList<T> values = new ArrayList<T>();
            for (int i = 0 ; i <count ; i++){
                T value = mOriginalValue.get(i);
                String valueText = value.toString();
                if(valueText != null && constraint!=null&&valueText.contains(constraint)){
                    values.add(value);
                }
            }
            results.values = values ;
            results.count = values.size();

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            mObject = (List<T>)results.values;
            if(results.count>0){
                notifyDataSetChanged();
            }

            else {
                notifyDataSetInvalidated();
            }
        }
    }
}
